#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include "lcd_2inch4.h"
#include "lcd_gui.h"
#include "fonts.h"
#ifdef USE_LGPIO
#include "sg_lgpio.h"
#else
#include <wiringPi.h>
#endif

uint16_t *image_buffer;

static void Show_Drawing_Page(void)
{
    Gui_Clear(WHITE);

    Gui_draw_str(12, 18, "2.4inch LCD", &Font24, BLACK, WHITE);
    Gui_draw_str(12, 52, "SEENGREAT", &Font24, BLUE, WHITE);
    Gui_draw_str(184, 52, "RGB565", &Font24, RED, WHITE);

    Gui_draw_line(15, 92, 145, 92, BLUE, 2, SOLID);
    Gui_draw_line(175, 92, 305, 92, RED, 2, DOTTED);

    Gui_draw_point(28, 122, YELLOW, 12);
    Gui_draw_rectangle(62, 108, 112, 142, BLUE, 2, EMPTY);
    Gui_draw_rectangle(132, 112, 172, 142, GREEN, 1, FULL);
    Gui_draw_circle(222, 125, 20, MAGENTA, 2, EMPTY);
    Gui_draw_circle(282, 125, 16, CYAN, 1, FULL);

    Gui_draw_str(18, 174, "POINT LINE SHAPE", &Font24, GREEN, WHITE);
    Gui_draw_str(34, 208, "0123456789", &Font24, BLACK, WHITE);

    Display(Image.img);
}

int main(void)
{
    const char *picture[] = {
        "./fruits24.bmp",
        "./birds.bmp",
        "./cat.bmp",
        "./fish.bmp",
        "./rabbit.bmp"
    };
    int i;

    image_buffer = (uint16_t *)malloc(LCD_SIZE);
    if (image_buffer == NULL)
        return -1;

#ifdef USE_LGPIO
    if (Lgpio_export() < 0)
    {
        free(image_buffer);
        return -1;
    }
#endif

    Image_Init(image_buffer, LCD_W, LCD_H, ROTATE_0, MIRROR_NONE);
    Lcd_Init();

    /* Repeat the same complete demonstration sequence as the 2inch CTP example. */
    while (1)
    {
        Show_Drawing_Page();
        delay(3000);

        for (i = 0; i < (int)(sizeof(picture) / sizeof(picture[0])); i++)
        {
            /* Gui_show_bmp clears the whole 320x240 frame before loading. */
            if (Gui_show_bmp(picture[i]) < 0)
                printf("skip picture: %s\r\n", picture[i]);
            Display(Image.img);
            delay(3000);
        }
    }

    free(image_buffer);
    return 0;
}
